﻿namespace NorthernLights
{
    using Microsoft.Phone.Shell;

    /// <summary>
    /// StateVariables class.
    /// </summary>
    public class StateVariables
    {
        #region Fields
        private static PhoneApplicationService appService = PhoneApplicationService.Current;
        #endregion
        
        #region Public methods
        /// <summary>
        /// Store value in State.
        /// </summary>
        /// <typeparam name="T">Object type.</typeparam>
        /// <param name="name">Key value.</param>
        /// <param name="value">Value to store.</param>
        public static void Set<T>(string name, T value)
        {
            appService.State[name] = value;
        }

        /// <summary>
        /// Get stored value.
        /// </summary>
        /// <typeparam name="T">Object type.</typeparam>
        /// <param name="name">Key to retrieve value.</param>
        /// <returns>Value matched to key.</returns>
        public static T Get<T>(string name)
        {
            T result = default(T);

            try
            {
                if (appService.State.ContainsKey(name))
                {
                    result = (T)appService.State[name];
                }
            }
            catch
            {
            }

            return result;
        }
        #endregion
    }
}
